package com.gexiaobao.pigeon.ui.auction.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentFullAuctionBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterSpecialAuction
import com.gexiaobao.pigeon.ui.auction.ActivityThematicList
import com.gexiaobao.pigeon.viewmodel.AuctionViewModel
import com.gexiaobao.pigeon.viewmodel.SharedViewModel
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2024/11/12 11:37
 * @Describe    : 今日结拍
 */
class FragmentFullAuction : BaseFragment<AuctionViewModel, FragmentFullAuctionBinding>() {

    private val mAdapterSpecial: AdapterSpecialAuction by lazy { AdapterSpecialAuction(arrayListOf()) }//拍卖专题
    private var sharedViewModel: SharedViewModel? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        sharedViewModel = ViewModelProvider(requireActivity())[SharedViewModel::class.java]
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.swipeRecyclerView.init(LinearLayoutManager(context), mAdapterSpecial)
            .addItemDecoration(SpaceItemDecoration(0, Util.dp2px(context, 8f)))

        sharedViewModel?.dataAuctions?.observe(viewLifecycleOwner) {
            if (it.isEmpty()) {
                mDatabind.llEmptyData.isVisible = true
                mDatabind.swipeRecyclerView.isVisible = false
            } else {
                mDatabind.llEmptyData.isVisible = false
                mDatabind.swipeRecyclerView.isVisible = true
                mAdapterSpecial.setList(it)
                mAdapterSpecial.notifyDataSetChanged()
            }
        }

        mAdapterSpecial.setOnItemClickListener { _, _, position ->
            startActivity<ActivityThematicList>("auctionId" to mAdapterSpecial.data[position].auctionId.toString())
        }

        mAdapterSpecial.setOnAuctionPigeonClickListener(object : AdapterSpecialAuction.OnAuctionPigeonClickListener {
            override fun onItemClick(position: Int) {
                startActivity<ActivityThematicList>("auctionId" to mAdapterSpecial.data[position].auctionId.toString())
            }
        })
    }

    companion object {
        fun newInstance(): FragmentFullAuction {
            val fragment = FragmentFullAuction()
            return fragment
        }
    }
}